﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DAO;
using DTO;

namespace QuanLyCuaHangVideo
{
    public partial class frmThue : Form
    {
        public frmThue()
        {
            InitializeComponent();
            lbUser.Text = "Người lập phiếu: " + frmMain.user;
        }

        private static float sum = 0;

        private void btnNhapCD_Click(object sender, EventArgs e)
        {
            List<Phim> dsPhim = PhimDAO.TimKiemMaPhim(txtIDCD.Text).ToList();
            List<KhachHang> dsKH = KhachHangDAO.TimKiemMaKH(txtIDKH.Text).ToList();
            dgvPhieuThue.Rows.Add(txtIDCD.Text, dsPhim[0].TieuDe, dsPhim[0].DonGia, dsKH[0].HoTen, txtSoLuong.Text, dtNgayThue.Value.ToString("dd/MM/yyyy"), dtNgayTra.Value.ToString("dd/MM/yyyy"));
            sum += (float)dsPhim[0].DonGia * Int32.Parse(txtSoLuong.Text);
            txtTong.Text = sum.ToString();
            txtIDCD.Text = string.Empty;
            txtSoLuong.Text = string.Empty;
            txtIDCD.Focus();
            txtIDKH.ReadOnly = true;
        }

        private void txtTra_TextChanged(object sender, EventArgs e)
        {
            float pay;
            if (txtTra.Text.Equals(string.Empty))
                pay = 0;
            else
                pay = float.Parse(txtTra.Text);
            float change = pay - sum;
            if (change < 0)
            {
                txtThieu.Text = Math.Abs(change).ToString();
                txtThua.Text = "0";
            }
            else
            {
                txtThua.Text = change.ToString();
                txtThieu.Text = "0";
            }
        }

        private void btnLuuPhieu_Click(object sender, EventArgs e)
        {
            txtIDKH.ReadOnly = false; txtIDKH.Text = string.Empty; txtIDCD.Text = string.Empty;
            txtSoLuong.Text = string.Empty; dtNgayThue.Value = DateTime.Now;
            dtNgayTra.Value = DateTime.Now; dgvPhieuThue.Rows.Clear();
            txtTong.Text = string.Empty; txtTra.Text = string.Empty;
            txtThua.Text = string.Empty; txtThieu.Text = string.Empty;
            sum = 0;
        }
    }
}
